<?php
include("../qcubed.inc.php");


class SeasonEditForm extends QForm {
    // Local instance of the SeasonMetaControl
    protected $mctSeason;

    // Controls for Season's Data Fields
    
    protected $txtName;
    protected $calDateFrom;
    protected $calDateTo;
    protected $chkActive;

    // Other ListBoxes (if applicable) via Unique ReverseReferences and ManyToMany References

    // Other Controls
    protected $btnSave;
    protected $btnDelete;
    protected $btnCancel;

    // Create QForm Event Handlers as Needed

//		protected function Form_Exit() {}
//		protected function Form_Load() {}
//		protected function Form_PreRender() {}

    protected function Form_Run() {
        // Security check for ALLOW_REMOTE_ADMIN
        // To allow access REGARDLESS of ALLOW_REMOTE_ADMIN, simply remove the line below
        QApplication::CheckRemoteAdmin();
    }

    protected function Form_Create() {
        // Use the CreateFromPathInfo shortcut (this can also be done manually using the SeasonMetaControl constructor)
        // MAKE SURE we specify "$this" as the MetaControl's (and thus all subsequent controls') parent
        $this->mctSeason = SeasonMetaControl::CreateFromPathInfo($this);

        // Call MetaControl's methods to create qcontrols based on Season's data fields
        
        $this->txtName = $this->mctSeason->txtName_Create();
        $this->calDateFrom = $this->mctSeason->calDateFrom_Create();
        $this->calDateTo = $this->mctSeason->calDateTo_Create();
        $this->chkActive = $this->mctSeason->chkActive_Create();

        // Create Buttons and Actions on this Form
        $this->btnSave = new QButton($this);
        $this->btnSave->Text = QApplication::Translate('Save');
        $this->btnSave->AddAction(new QClickEvent(), new QAjaxAction('btnSave_Click'));
        $this->btnSave->CausesValidation = true;

        $this->btnCancel = new QButton($this);
        $this->btnCancel->Text = QApplication::Translate('Cancel');
        $this->btnCancel->AddAction(new QClickEvent(), new QAjaxAction('btnCancel_Click'));

        $this->btnDelete = new QButton($this);
        $this->btnDelete->Text = QApplication::Translate('Delete');
        $this->btnDelete->AddAction(new QClickEvent(), new QConfirmAction(QApplication::Translate('Are you SURE you want to DELETE this') . ' ' . QApplication::Translate('Season') . '?'));
        $this->btnDelete->AddAction(new QClickEvent(), new QAjaxAction('btnDelete_Click'));
        $this->btnDelete->Visible = $this->mctSeason->EditMode;
    }

    /**
     * This Form_Validate event handler allows you to specify any custom Form Validation rules.
     * It will also Blink() on all invalid controls, as well as Focus() on the top-most invalid control.
     */
    protected function Form_Validate() {
        // By default, we report that Custom Validations passed
        $blnToReturn = true;

        // Custom Validation Rules
        // TODO: Be sure to set $blnToReturn to false if any custom validation fails!
        if ($this->calDateFrom->DateTime && $this->calDateTo->DateTime){
            //$dtfrom = new QDateTime();
            //$x = new QDateTime();
            $dtfrom = $this->calDateFrom->DateTime;
            $dtto   = $this->calDateTo->DateTime;
            
            $dtDiff= $dtto->Difference($dtfrom);
            
            if ( $dtDiff->IsNegative() ){
                $this->calDateFrom->Warning = "Date From cant be earlier than Date To";
                $blnToReturn=false;
            }
            //should add 1314871
            elseif  ($dtDiff->Seconds >= 32871797 ) {
                $this->calDateFrom->Warning = "Season Cant be more than a year";
                $blnToReturn=false;
            }
            
            if ( $this->chkActive->Checked){
                $SeasonsArray = Season::LoadArrayByActive(TRUE);

                if (count($SeasonsArray) > 0) {
                    $Idthis = $this->mctSeason->__get('Season')->Id;

                    if ( $SeasonsArray[0]->Id <> $Idthis ){
                        $this->chkActive->Warning = "Another season is active. disable first";
                        $blnToReturn=false;
                    }
                }
            }
        }

        $blnFocused = false;
        foreach ($this->GetErrorControls() as $objControl) {
            // Set Focus to the top-most invalid control
            if (!$blnFocused) {
                $objControl->Focus();
                $blnFocused = true;
            }

            // Blink on ALL invalid controls
            $objControl->Blink();
        }

        return $blnToReturn;
    }

    // Button Event Handlers

    protected function btnSave_Click($strFormId, $strControlId, $strParameter) {
        // Delegate "Save" processing to the SeasonMetaControl
        $this->mctSeason->SaveSeason();
        $this->RedirectToListPage();
    }

    protected function btnDelete_Click($strFormId, $strControlId, $strParameter) {
        // Delegate "Delete" processing to the SeasonMetaControl
        $this->mctSeason->DeleteSeason();
        $this->RedirectToListPage();
    }

    protected function btnCancel_Click($strFormId, $strControlId, $strParameter) {
        $this->RedirectToListPage();
    }

    // Other Methods

    protected function RedirectToListPage() {
        QApplication::Redirect(__MASTERDATA__ . '/season_list.php');
    }
}
SeasonEditForm::Run('SeasonEditForm','tpl/season_edit.tpl.php');
?>
